﻿using System.Threading;
using System.Threading.Tasks;
using ESRI.ArcGIS.Client.Tasks;

namespace ESRI.ArcGIS.Client.Async
{
    public static partial class ImageServiceIdentifyTaskExtensions
    {
        #region ExecuteTaskAsync

        /// <summary>
        /// Executes an IdentifyImageServiceTask against an ArcGIS Server image service layer. The result is returned as an array of features.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="parameters">Specifies the criteria used to identify the features of an image service layer.</param>
        /// <returns></returns>
        public static Task<ImageServiceIdentifyTaskResult> ExecuteTaskAsync(this ImageServiceIdentifyTask task,
            ImageServiceIdentifyParameters parameters)
        {
            return ExecuteTaskAsync(task, parameters, CancellationToken.None);
        }

        /// <summary>
        /// Executes an IdentifyImageServiceTask against an ArcGIS Server image service layer. The result is returned as an array of features.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="parameters">Specifies the criteria used to identify the features of an image service layer.</param>
        /// <param name="cancellationToken">Cancellation token for cancelling task.</param>
        /// <returns></returns>
        public static Task<ImageServiceIdentifyTaskResult> ExecuteTaskAsync(this ImageServiceIdentifyTask task,
            ImageServiceIdentifyParameters parameters, CancellationToken cancellationToken)
        {
            return task
                .HandleTask<ImageServiceIdentifyTask, ImageServiceIdentifyEventArgs, ImageServiceIdentifyTaskResult>(
                    t => t.ExecuteAsync(parameters),
                    (t, e) => t.ExecuteCompleted += e,
                    (t, e) => t.ExecuteCompleted -= e,
                    e => new ImageServiceIdentifyTaskResult(e),
                    cancellationToken);
        }

        #endregion

    }
}